// 消息提示
export function $messageTip(text) {
    const infoDom = $('.info-warning')[0]
    infoDom.style.display = 'block'
    infoDom.innerText = text
    setTimeout(() => {
        infoDom.style.display = 'none'
    }, 1000)
}

export const axiosBaseUrl = 'http://localhost:9000'

// axios 请求封装
import { Axios } from '../lib/axios.js';
export function $axios() {

    const axios = new Axios({
        baseURL: axiosBaseUrl,
        timeout: 5000
    })

    axios.interceptors.request.use(
        config => {
            const token = sessionStorage.getItem('frontToken');
            if (token) {
                config.headers['authorization'] = token
            }
            return config;
        },
        error => {
            return Promise.reject(error);
        }
    );

    axios.interceptors.response.use(
        response => {
            if (response.data) {
                const data = JSON.parse(response.data)
                if (data.code === 401) {
                    $messageTip('登录过期，请重新登录')
                    sessionStorage.removeItem('frontToken')
                    sessionStorage.removeItem('frontUserInfo')
                    window.location.href = './login.html'
                }
            }
            
            return response;
        }
    )
    return axios

}